package com.expense.app.admin.common;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.entity.params.PaginationParam;
import com.expense.common.entity.params.SortParam;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.auth.AdminUser;
import com.expense.common.model.base.BaseModel;
import com.expense.common.util.UserUtils;
import org.beetl.sql.core.engine.PageQuery;

import java.util.Arrays;
import java.util.Date;

public class BaseService {

    /**
     * 组装分页PageQuery
     * @param paginationParam
     * @param sortParams
     * @return
     */
    public PageQuery initPageQuery(PaginationParam paginationParam, SortParam... sortParams) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNumber(paginationParam.getPage());
        pageQuery.setPageSize(paginationParam.getPageSize());
        String orderByStr = "";
        if(ArrayUtil.isEmpty(sortParams)) {
            orderByStr = "id desc";
        }else {
            SortParam sortParam = sortParams[0];
            SortParam[] defailtParams = ArrayUtil.remove(sortParams,0);
            if(sortParam != null && StrUtil.isNotBlank(sortParam.getSort())) {
                if(SortParam.DESC.equals(sortParam.getOrder())) {
                    orderByStr = StrUtil.toUnderlineCase(sortParam.getSort()) +" DESC";
                } else {
                    orderByStr = StrUtil.toUnderlineCase(sortParam.getSort()) +" ASC";
                }
            }else {
                if(ArrayUtil.isEmpty(defailtParams)) orderByStr = "id desc";
                else {
                    StringBuilder strs = new StringBuilder();
                    Arrays.asList(defailtParams).stream().forEach(defailtParam -> {
                        if(SortParam.DESC.equals(defailtParam.getOrder())) {
                            strs.append(",").append(StrUtil.toUnderlineCase(defailtParam.getSort())).append(" DESC");
                        } else {
                            strs.append(",").append(StrUtil.toUnderlineCase(defailtParam.getSort())).append(" ASC");
                        }
                    });
                    orderByStr = strs.toString().substring(1);
                }
            }
        }
        pageQuery.setOrderBy(orderByStr);
        return pageQuery;
    }

    /**
     * 判断对象是否为空
     * @param baseModel
     * @return
     */
    public boolean isNullObject(BaseModel baseModel) {
        return (baseModel == null || baseModel.getId() == null || baseModel.getId() <= 0);
    }

    /**
     * 设置基础信息字段
     * @param baseModel
     * @return
     */
    public BaseModel setBaseInfo (BaseModel baseModel) {
        Object object = UserUtils.getUser();
        AdminUser adminUser = (AdminUser) object;
        if(adminUser == null) return null;
        if(baseModel.getId() == null || baseModel.getId() == 0) {
            baseModel.setCreatorId(adminUser.getId());
            baseModel.setCreateAt(new Date());
            baseModel.setDeleted(DeletedStatus.UN_DELETED);
        }else {
            baseModel.setModifierId(adminUser.getId());
            baseModel.setModifyAt(new Date());
        }
        return baseModel;
    }

    public AdminUser getCurrentUser() {
        Object object = UserUtils.getUser();
        AdminUser adminUser = (AdminUser)object;
        if(adminUser == null) throw new PlatformException("用户不存在");
        return adminUser;
    }

}
